package eclipse.lib.sockets;
import eclipse.server.*;
import eclipse.lib.player.Account;
import eclipse.lib.scripting.*;
/**
 * The ServerDataHandle class handles all TCP messages sent to the Server
 */
/*
 * Copyright (c) 2009, TOD Productions All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 *	- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 *	- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 *	-Neither the name of the organization nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */
public final class ServerDataHandle
{
	private static final char msg_REQUESTMAX 		= 0;
	private static final char msg_REQUESTNEWACCOUNT	= 1;
	private static final char msg_SENDCHARPOS		= 2;
	private static final char msg_REQUESTLOGIN		= 3;
	/** Default sent "error 404, file does not exist" message */
	public static final String DEFAULT404 = "" +
		"<html>" +
			"<body>" +
				"Page not found!" +
			"</body>" +
		"</html>";			
	/**
	 * Processes a message sent to the Server
	 * @param index Socket that the message came from
	 * @param netMsg Message recieved
	 */
	public static final void process(int index, String netMsg)
	{	
		if(netMsg.length() == 0)
			return;				
		else if(netMsg.charAt(0) == msg_REQUESTMAX)
			sendMax(index);
		else if(netMsg.charAt(0) == msg_REQUESTNEWACCOUNT)
			sendNewAccountConf(index, netMsg);
		else if(netMsg.charAt(0) == msg_SENDCHARPOS)
			sendCharPosToAll(index, netMsg);
		else if(netMsg.charAt(0) == msg_REQUESTLOGIN)
			sendLoginConf(index, netMsg);
		else 
			unknownRequest(index, netMsg);
			
	}
	public static final void unknownRequest(int index, String netMsg)
	{
		JScript.evaluate("unknownRequest", index, netMsg);
	}	
	/**
	 * Sends the character position to all players on the same map.
	 * @param index Client where the message came from
	 * @param netMsg Message recieved
	 */
	public static final void sendCharPosToAll(int index, String netMsg)
	{		
		//TODO: send only to same map
		String msg = msg_SENDCHARPOS + "";
		final int dir 	= ByteInterpreter.getIntFromString(netMsg, 1);
		final int frame	= ByteInterpreter.getIntFromString(netMsg, 3);
		final float xPos 	= ByteInterpreter.getFloatFromString(netMsg, 5);
		final float yPos 	= ByteInterpreter.getFloatFromString(netMsg, 7);
		final float zPos 	= ByteInterpreter.getFloatFromString(netMsg, 9);
		
		msg = msg + ByteInterpreter.appendIntegerToString(index);
		msg = msg + ByteInterpreter.appendIntegerToString(dir);
		msg = msg + ByteInterpreter.appendIntegerToString(frame);
		msg = msg + ByteInterpreter.appendFloatToString(xPos);
		msg = msg + ByteInterpreter.appendFloatToString(yPos);
		msg = msg + ByteInterpreter.appendFloatToString(zPos);
		msg = msg + "\n";
		sendMsgToMapBut(msg, Database.player[index].character.cMap, index);				
	}
	/** Send the maximum data to the client */
	public static final void sendMax(int index)
	{
		String msgCode = msg_REQUESTMAX + "";
		msgCode = msgCode + ByteInterpreter.appendIntegerToString(Database.MAXPLAYERS);
		msgCode = msgCode + ByteInterpreter.appendIntegerToString(Database.MAXITEMS);
		msgCode = msgCode + ByteInterpreter.appendIntegerToString(Database.MAXMAPS);
		msgCode = msgCode + ByteInterpreter.appendIntegerToString(Database.MAXNPCS);
		msgCode = msgCode + ByteInterpreter.appendIntegerToString(Database.MAXSHOPS);
		msgCode = msgCode + ByteInterpreter.appendIntegerToString(Database.MAXSPELLS);
		msgCode = msgCode + ByteInterpreter.appendIntegerToString(Database.MAXCLASSES);
		msgCode = msgCode + ByteInterpreter.appendIntegerToString(index);
		Database.client[index].sendMsg(msgCode + "\n");
	}
	/** Sends the confirmation that the account was created */
	public static final void sendNewAccountConf(int index, String netMsg)
	{
		final String split 	= eclipse.Constants.SEP_CHAR + "";		
			  String msgCode = msg_REQUESTNEWACCOUNT + "";
		final String parse[] 	= netMsg.split(split);	
		
		try
		{
			final String uName 	= parse[0].replace(msg_REQUESTNEWACCOUNT + "", "");		
			final String pWord	= parse[1];
			final Account nAcc = new Account();
			
			if(nAcc.makeAccount(uName, pWord))
				msgCode = msgCode + "t";
			else
				msgCode = msgCode + "f";
		}
		catch(Exception e)
		{
			msgCode = msgCode + "f";
		}		
		Database.client[index].sendMsg(msgCode + "\n");
	}
	/**
	 *  Sends a message to everyone but the specified player.
	 *  @param msg Message to send
	 *  @param exclude Player not to recieve the message
	 */ 
	public static final void sendMsgToAllBut(String msg, int exclude)
	{
		for(int i = 0; i < Database.MAXPLAYERS; i++)
		{
			if(i != exclude)
			{
				if(Database.client[i] != null)			
				{
					Database.client[i].sendMsg(msg);
				}
			}
		}
	}
	/**
	 * Sends a message to everyone on a single map
	 * @param msg Message to be sent
	 * @param map Map to recieve the message
	 */
	public static final void sendMsgToMap(String msg, int map)
	{
		for(int i = 0; i < Database.MAXPLAYERS; i++)
		{
			if(Database.client[i] != null)
			{
				if(Database.player[i].character.cMap == map)
				{
					Database.client[i].sendMsg(msg);
				}
			}
		}
	}
	/**
	 * Sends a message to the map excluding a specific player
	 * @param msg Message to be sent
	 * @param map Map to recieve the messag
	 * @param exclude Person not to recieve the message
	 */
	public static final void sendMsgToMapBut(String msg, int map, int exclude)
	{
		for(int i = 0; i < Database.MAXPLAYERS; i++)
		{
			if(Database.client[i] != null)
			{
				if(i != exclude)
				{
					if(Database.player[i].character.cMap == map)
					{
						Database.client[i].sendMsg(msg);
					}
				}
			}
		}
	}
	/**
	 * Sends a message to everyone connected.
	 * @param msg Message to be sent
	 */
	public static final void sendMsgToAll(String msg)
	{
		for(int i = 0; i < Database.MAXPLAYERS; i++)
		{
			if(Database.client[i] != null)
				Database.client[i].sendMsg(msg);
		}		
	}
	public static final void sendLoginConf(int index, String netMsg)
	{
		final String split		= eclipse.Constants.SEP_CHAR + "";
			  String msgCode	= msg_REQUESTLOGIN + "";
		final String parse[]	= netMsg.split(split);
		
		try
		{
			final String uName	= parse[0].replace(msg_REQUESTLOGIN + "", "");
			final String pWord	= parse[1];
			final Account pAcc	= new Account();
			
			if(pAcc.loadAccount(index, uName, pWord))
				msgCode = msgCode + "t";
			else
				msgCode = msgCode + "f";
		}
		catch(Exception e)
		{
			msgCode = msgCode + "f";
		}
		Database.client[index].sendMsg(msgCode + "\n");
		JScript.evaluate("onLogin", index);
	}	
}
